package com.sunny.ynhz.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.FileAdapter;
import com.sunny.ynhz.adapter.OrganizationPhotoAdapter;
import com.sunny.ynhz.adapter.ProgressProblemAdapter;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.NetUrl;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.OneRiverOneRuleDeatailBean;
import com.sunny.ynhz.bean.WarpFileMsg;
import com.sunny.ynhz.ui.base.AppBaseTitleBarActivity;
import com.sunny.ynhz.utils.GridDividerItemDecoration;
import com.sunny.ynhz.utils.NumberChangeToChinese;
import com.sunny.ynhz.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/6/1.
 */

public class OneRiverActivity extends AppBaseTitleBarActivity {

    private RecyclerView rl_group_progress;
    private LinearLayout ll_content;
    List<OneRiverOneRuleDeatailBean.ListBean.RiverMeasureBean> mList = new ArrayList<>();
    private String id;
    @BindView(R.id.tv_move_task)
    TextView tv_move_task;
    @BindView(R.id.tv_move_content)
    TextView tv_move_content;
    @BindView(R.id.tv_zenren_unit)
    TextView tv_zenren_unit;
    @BindView(R.id.tv_orgin)
    TextView tv_orgin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_problem);
        ll_content = findViewById(R.id.ll_content);
        id = getIntent().getStringExtra("id");
        setTitle("任务详情");
        getData();
    }

    public void getData() {
        HttpManager.getInstance().getListRiverOneRuleMsgById(id).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(this, new ApiSubscriber.HttpOnResultListener<OneRiverOneRuleDeatailBean>() {
            @Override
            public void onNext(OneRiverOneRuleDeatailBean oneRiverOneRuleDeatailBean) {
                OneRiverOneRuleDeatailBean.ListBean list = oneRiverOneRuleDeatailBean.getList();
                String accountabilityUnit = list.getAccountabilityUnit();
                String content = list.getContent();
                String competentDepartment = list.getCompetentDepartment();
                String target = list.getTarget();
                tv_move_task.setText(target);
                tv_move_content.setText(content);
                tv_orgin.setText(competentDepartment);
                tv_zenren_unit.setText(accountabilityUnit);
                List<OneRiverOneRuleDeatailBean.ListBean.RiverMeasureBean> riverMeasure = list.getRiverMeasure();
                mList.addAll(riverMeasure);
                exchageDataForm();

            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onstart() {

            }
        }));

    }


    public void exchageDataForm() {
        int size = mList.size();
        for (int i = 0; i < size; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_handle_problem, null);
            OneRiverOneRuleDeatailBean.ListBean.RiverMeasureBean riverMeasureBean = mList.get(i);
            TextView tv_del_name = view.findViewById(R.id.tv_cuoshi_name);
            TextView tv_cuo_shi = view.findViewById(R.id.tv_cuo_shi);
            rl_group_progress = view.findViewById(R.id.rl_group_progress);
            tv_cuo_shi.setText("措施" + NumberChangeToChinese.numberToChinese(i + 1));
            tv_del_name.setText(riverMeasureBean.getMeasureName());
            String fileurl = riverMeasureBean.getRiverMeasureFile().getFileurl();
            if (!fileurl.isEmpty()) {
                String fileSubUrl = fileurl.substring(1, fileurl.length());
                String[] split1 = fileSubUrl.split("#");
                List<WarpFileMsg> list = new ArrayList<>();
                WarpFileMsg warpFileMsg = new WarpFileMsg();
                warpFileMsg.setFileName("文件名");
                warpFileMsg.setFileTime("发布时间");
                warpFileMsg.setType("操作");
                warpFileMsg.setLoading(-1);
                list.add(warpFileMsg);
                for (int k = 0; k < split1.length; k++) {
                    String file = split1[k];
                    String[] split = file.split("/");
                    String fileTime = split[2];
                    String fileName = split[3].substring(32, split[3].length());
                    WarpFileMsg warpFileMsg1 = new WarpFileMsg();
                    warpFileMsg1.setFileName(fileName);
                    warpFileMsg1.setLoadUrl(NetUrl.loadBaseUrl + file);
                    int progressValue = Integer.parseInt(SPUtil.get(NetUrl.loadBaseUrl + file, 0).toString());
                    if (progressValue == 100) {
                        warpFileMsg1.setLoading(2);
                    } else if (progressValue == 0) {
                        warpFileMsg1.setLoading(0);
                    } else {
                        warpFileMsg1.setLoading(1);
                    }
                    warpFileMsg1.setProgress(progressValue);
                    warpFileMsg1.setFileTime(fileTime);
                    warpFileMsg1.setType("下载");
                    list.add(warpFileMsg1);
                }
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                };
                RecyclerView rl_about_file = view.findViewById(R.id.rl_about_file);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                rl_about_file.setLayoutManager(linearLayoutManager);
                rl_about_file.setAdapter(new FileAdapter(this, list, linearLayoutManager));
            }
            String imgurl = riverMeasureBean.getRiverMeasureFile().getImgurl();
            List<String> imgUrls = new ArrayList<>();
            if (!imgurl.isEmpty()) {
                String subImgs = imgurl.substring(1, imgurl.length());
                String[] spliturls = subImgs.split("#");
                for (int m = 0; m < spliturls.length; m++) {
                    String imgs = spliturls[m];
                    imgUrls.add(imgs);
                }
                RecyclerView rl_organization_photo = view.findViewById(R.id.rl_organization_photo);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                };
                rl_organization_photo.setLayoutManager(gridLayoutManager);
                OrganizationPhotoAdapter organizationPhotoAdapter = new OrganizationPhotoAdapter(this, imgUrls);
                rl_organization_photo.addItemDecoration(new GridDividerItemDecoration(20, Color.WHITE));
                rl_organization_photo.setAdapter(organizationPhotoAdapter);
            }
            String mobilizeSchedule = riverMeasureBean.getMobilizeSchedule();//动员进度
            String gropingInvestigation = riverMeasureBean.getGropingInvestigation();//摸牌进度调查
            String renovateSchedule = riverMeasureBean.getRenovateSchedule();//五政治制度
            String publicityEducation = riverMeasureBean.getPublicityEducation();//宣传教育
            String quantitativeAssessment = riverMeasureBean.getQuantitativeAssessment();//量化考察
            String basicImprovement = riverMeasureBean.getBasicImprovement();//基本改善目标
            String measureprogressBar = riverMeasureBean.getMeasureprogressBar();
            List<String> listProgress = new ArrayList<>();
            listProgress.add(mobilizeSchedule);
            listProgress.add(gropingInvestigation);
            listProgress.add(renovateSchedule);
            listProgress.add(publicityEducation);
            listProgress.add(quantitativeAssessment);
            listProgress.add(basicImprovement);
            listProgress.add(measureprogressBar);
            ProgressProblemAdapter progressProblemAdapter = new ProgressProblemAdapter(this, listProgress);
            LinearLayoutManager linearLayoutManagerFile = new LinearLayoutManager(this) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            linearLayoutManagerFile.setOrientation(LinearLayoutManager.VERTICAL);
            rl_group_progress.setLayoutManager(linearLayoutManagerFile);
            rl_group_progress.setAdapter(progressProblemAdapter);
            ll_content.addView(view);
        }
    }
}

